//
//  LYSelectCircleViewController.m
//  artist
//
//  Created by laotang on 16/5/15.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYSelectCircleViewController.h"
#import "LYSelectedSubjectCollectionCell.h"
#import "LYCircleListModel.h"
static NSString *const SelectedClassesViewControllerCollectionID = @"SelectedClassesViewControllerCollectionID";
@interface LYSelectCircleViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionVeiw;
@property (nonatomic, strong) NSArray<LYCircleListModel *> * circleList;
@property (nonatomic, strong) LYCircleListModel * selectedCircleModel;

@property(nonatomic, copy) NSString *content;
@property(nonatomic, copy) NSString *images;

@end

@implementation LYSelectCircleViewController

- (instancetype)initWithContent:(NSString *)string imagees:(NSString *)images;{
   self =  [self init];
    if(self){
        self.content = string;
        self.images = images;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionVeiw];
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(publicClass)];
    self.navigationItem.rightBarButtonItem = button;
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.leftBarButtonItem = leftButton;
    self.title = @"发布主题";
    [self requestData];
}

- (void)requestData{
    
    WS(weakSelf)
    [LYRequestTool acquirCircleListWithToken:[AppDelegate shareInstance].token type:@"all" success:^(NSArray<LYCircleListModel *> *listModel) {
        
        weakSelf.circleList = listModel;
        [weakSelf.collectionVeiw reloadData];
        
    } failure:^(NSString *code, NSString *message) {
        [SVProgressHUD showErrorWithStatus:message];
    }];
}

- (UICollectionView *)collectionVeiw{
    
    if(!_collectionVeiw){
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        CGFloat width = LYScreenWidth / 3  - 30;
        flowLayout.itemSize = CGSizeMake(width, 40);

        _collectionVeiw = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionVeiw.backgroundColor = [UIColor whiteColor];
        [_collectionVeiw registerClass:[LYSelectedSubjectCollectionCell class] forCellWithReuseIdentifier:SelectedClassesViewControllerCollectionID];
        _collectionVeiw.delegate = self;
        _collectionVeiw.dataSource = self;
    }
    return _collectionVeiw;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.circleList.count;

}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LYSelectedSubjectCollectionCell *cell = (LYSelectedSubjectCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:SelectedClassesViewControllerCollectionID forIndexPath:indexPath];
    LYCircleListModel *subject = self.circleList[indexPath.item];
    [cell setTitle:subject.name];
    if (self.selectedCircleModel == subject){
        cell.classesTitleButton.selected = YES;
    }else{
        cell.classesTitleButton.selected = NO;
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    self.selectedCircleModel = self.circleList[indexPath.row];
    [collectionView reloadData];
    
}


- (void)publicClass{
    
    WS(weakSelf)
    [LYRequestTool publicPosttWithToken:[AppDelegate shareInstance].token circle_id:self.selectedCircleModel.circle_id content:self.content images:self.images success:^(NSString *topicId) {
        [SVProgressHUD showSuccessWithStatus:@"发布成功"];
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
    } failure:^(NSString *code, NSString *message) {
        [SVProgressHUD showSuccessWithStatus:message];

    }];
    
}
@end
